package com.dawn.module.mp.service.msgtemplatelog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.dawn.module.mp.controller.admin.msgtemplatelog.vo.*;
import com.dawn.module.mp.dal.dataobject.msgtemplatelog.MsgTemplateLogDO;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.util.object.BeanUtils;

import com.dawn.module.mp.dal.mysql.msgtemplatelog.MsgTemplateLogMapper;

import static com.dawn.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.dawn.module.mp.enums.ErrorCodeConstants.*;

/**
 * 模板消息记录 Service 实现类
 *
 * @author mamba 4 left
 */
@Service
@Validated
public class MsgTemplateLogServiceImpl implements MsgTemplateLogService {

    @Resource
    private MsgTemplateLogMapper msgTemplateLogMapper;

    @Override
    public Long createMsgTemplateLog(MsgTemplateLogSaveReqVO createReqVO) {
        // 插入
        MsgTemplateLogDO msgTemplateLog = BeanUtils.toBean(createReqVO, MsgTemplateLogDO.class);
        msgTemplateLogMapper.insert(msgTemplateLog);
        // 返回
        return msgTemplateLog.getId();
    }

    @Override
    public void updateMsgTemplateLog(MsgTemplateLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMsgTemplateLogExists(updateReqVO.getId());
        // 更新
        MsgTemplateLogDO updateObj = BeanUtils.toBean(updateReqVO, MsgTemplateLogDO.class);
        msgTemplateLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMsgTemplateLog(Long id) {
        // 校验存在
        validateMsgTemplateLogExists(id);
        // 删除
        msgTemplateLogMapper.deleteById(id);
    }

    private MsgTemplateLogDO validateMsgTemplateLogExists(Long id) {
        MsgTemplateLogDO msgTemplateLogDO = msgTemplateLogMapper.selectById(id);
        if (msgTemplateLogDO == null) {
            throw exception(MSG_TEMPLATE_LOG_NOT_EXISTS);
        }
        return msgTemplateLogDO;
    }

    @Override
    public MsgTemplateLogDO getMsgTemplateLog(Long id) {
        return msgTemplateLogMapper.selectById(id);
    }

    @Override
    public PageResult<MsgTemplateLogDO> getMsgTemplateLogPage(MsgTemplateLogPageReqVO pageReqVO) {
        return msgTemplateLogMapper.selectPage(pageReqVO);
    }

}
